package cn.com.thinvent.dispatch.service.biz.impl;

import cn.com.thinvent.dispatch.dao.StudyManagementMapper;
import cn.com.thinvent.dispatch.dao.CourseDetailsMapper;
import cn.com.thinvent.dispatch.entity.StudyManagement;
import cn.com.thinvent.dispatch.entity.CourseDetails;
import cn.com.thinvent.dispatch.entity.request.StudyManagementRequest;
import cn.com.thinvent.dispatch.entity.request.CourseDetailsRequest;
import cn.com.thinvent.dispatch.entity.response.PagedGridResult;
import cn.com.thinvent.dispatch.service.biz.StudyManagementService;
import cn.com.thinvent.dispatch.service.biz.util.DataConvertUtils;
import cn.com.thinvent.dispatch.service.biz.util.KeyGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StudyManagementServiceImpl implements StudyManagementService {

    @Autowired
    private StudyManagementMapper studyManagementMapper;

    @Autowired
    private CourseDetailsMapper courseDetailsMapper;

    @Override
    public StudyManagement save(StudyManagementRequest studyManagementRequest) {
        Date date = new Date();
        StudyManagement studyManagement = DataConvertUtils.convertDTO2DO(studyManagementRequest, new StudyManagement());
        // 转换 StudyManagementRequest 到 StudyManagement 实体
        studyManagement.setId(KeyGenerator.generateKey());
        studyManagement.setCreatedAt(date);
        studyManagement.setUpdatedAt(date);
        studyManagementMapper.insertStudy(studyManagement);

        // 处理课程详情信息
        if (studyManagementRequest.getCourseDetailsRequests() != null) {
            for (CourseDetailsRequest detailsRequest : studyManagementRequest.getCourseDetailsRequests()) {
                CourseDetails courseDetails = DataConvertUtils.convertDTO2DO(detailsRequest, new CourseDetails());
                courseDetails.setCourseId(studyManagement.getId()); // 关联 StudyManagement 的 ID
                courseDetails.setId(KeyGenerator.generateKey());
                courseDetails.setCreatedAt(date);
                courseDetails.setUpdatedAt(date);
                courseDetailsMapper.insertCourseDetails(courseDetails);
            }
        }

        return studyManagement;
    }

    @Override
    public void deleteById(String id) {
        // 删除学习管理记录
        studyManagementMapper.deleteStudyById(id);
        // 删除相关的课程详情记录
        courseDetailsMapper.deleteByCourseId(id);
    }

    @Override
    public StudyManagement update(StudyManagementRequest studyManagementRequest) {
        Date date = new Date();
        // 转换 StudyManagementRequest 到 StudyManagement 实体
        StudyManagement studyManagement = DataConvertUtils.convertDTO2DO(studyManagementRequest, new StudyManagement());
        studyManagement.setUpdatedAt(date);
        studyManagementMapper.updateStudy(studyManagement);

        // 先删除原有的课程详情记录
        courseDetailsMapper.deleteByCourseId(studyManagement.getId());

        // 然后插入新的课程详情记录
        if (studyManagementRequest.getCourseDetailsRequests() != null) {
            for (CourseDetailsRequest detailsRequest : studyManagementRequest.getCourseDetailsRequests()) {
                CourseDetails courseDetails = DataConvertUtils.convertDTO2DO(detailsRequest, new CourseDetails());
                courseDetails.setId(KeyGenerator.generateKey());
                courseDetails.setCourseId(studyManagement.getId()); // 关联 StudyManagement 的 ID
                courseDetails.setCreatedAt(date);
                courseDetails.setUpdatedAt(date);
                courseDetailsMapper.insertCourseDetails(courseDetails);
            }
        }

        return studyManagement;
    }

    @Override
    public StudyManagementRequest findById(String id) {
        StudyManagement studyManagement = studyManagementMapper.selectStudyById(id);
        StudyManagementRequest studyManagementRequest = DataConvertUtils.convertDO2DTO(studyManagement, StudyManagementRequest.class);

        // 查询并设置课程详情信息
        List<CourseDetailsRequest> courseDetailsRequests = new ArrayList<>();
        List<CourseDetails> courseDetailsList = courseDetailsMapper.selectCourseDetailsByCourseId(id);
        if (courseDetailsList != null) {
            DataConvertUtils.convertDOList2DTOList(courseDetailsList, courseDetailsRequests, CourseDetailsRequest.class);
        }
        studyManagementRequest.setCourseDetailsRequests(courseDetailsRequests);
        return studyManagementRequest;
    }

    @Override
    public PagedGridResult<StudyManagement> getStudyList(int page, int pageSize, String courseName, Date startDate, Date endDate) {
        int offset = (page - 1) * pageSize;
        List<StudyManagement> list = studyManagementMapper.selectStudyList(offset, pageSize, courseName, startDate, endDate);
        long totalRecords = studyManagementMapper.countStudyList(courseName, startDate, endDate);
        int totalPages = (int) Math.ceil((double) totalRecords / pageSize);

        PagedGridResult<StudyManagement> pagedResult = new PagedGridResult<>();
        pagedResult.setPage(page);
        pagedResult.setTotal(totalPages);
        pagedResult.setRecords(totalRecords);
        pagedResult.setRows(list);

        return pagedResult;
    }
}
